﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Drawing.Imaging;
using BFOAuction.BLL.Register;
using BFOAuction.DTO.Register;
using BFOAuction.BLL.Product;
using BFOAuction.DTO.Product;

namespace BFOAuction
{
    public partial class DangKy : System.Web.UI.Page
    {
        #region Khai báo biến
        AppFormat.MyError objError = null;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        #region Account

        private bool Ins_Info_Account()
        {
            objError = new AppFormat.MyError();
            Register objAccount = new Register();
            objAccount.StrTenDangNhap = txttenDangNhap.Text;
            objAccount.StrMatKhau = txtMatKhau.Text;

            objError = new BLL_Register().Insert_Account(objAccount);

            if (objError.IsError)
            {
                AppFormat.ShowMessage.ShowMsg(objError.MessageError);
                return false;
            }
           
            ClearData();

            return true;
        }

        #endregion

        #region Customer
        private bool Ins_Info_Customer()
        {
            objError = new AppFormat.MyError();
            Register objCustomer = new Register();
            objCustomer.StrHoTen = txtHoTen.Text.Trim();
            objCustomer.StrGioiTinh = drdGioiTinh.SelectedItem.ToString();

            long CMND;
            long.TryParse(txtCMND.Text.Trim(), out CMND);
            objCustomer.LCMND = CMND;

            objCustomer.StrDiaChi = txtDiaChi.Text;
            objCustomer.StrThanhPho = drdThanhPho.SelectedItem.ToString();

            objCustomer.StrEmail = txtEmail.Text.Trim();

            long DienThoai;
            long.TryParse(txtDienThoai.Text.Trim(),out DienThoai);
            objCustomer.LDienThoai = DienThoai;

            objError = new BLL_Register().Insert_Info_Customer(objCustomer);

            if (objError.IsError)
            {
                AppFormat.ShowMessage.ShowMsg(objError.MessageError);
                return false;
            }

            return true;
        }
        #endregion

        private int countUser()
        {
            string cn = DataProvider.DataProvider.Connection();
            SqlConnection con = new SqlConnection(cn);
            con.Open();
            string strSql = "Select count(*) from Account where Username = '" + txttenDangNhap.Text + "' ";
            SqlCommand UserExist = new SqlCommand(strSql, con);
            int temp = Convert.ToInt32(UserExist.ExecuteScalar().ToString());
            con.Close();

            return temp;
        }

        #region Payment

        private bool Ins_Info_Payment()
        {
            objError = new AppFormat.MyError();
            Register objCustomer = new Register();
            
            long SoTaiKhoan;
            long.TryParse(txtSoTaiKhoan.Text.Trim(), out SoTaiKhoan);
            objCustomer.LCMND = SoTaiKhoan;
            
            long SoDu;
            long.TryParse(txtSoDu.Text.Trim(), out SoDu);
            objCustomer.LDienThoai = SoDu;

            objError = new BLL_Register().Insert_Info_Payment(objCustomer);

            if (objError.IsError)
            {
                AppFormat.ShowMessage.ShowMsg(objError.MessageError);
                return false;
            }

            return true;
        }

        #endregion

        #region ClearData
        protected void ClearData()
        {
            txttenDangNhap.Text = "";
            txtMatKhau.Text = "";
            txtNhacLaiMatKhau.Text = "";
            txtHoTen.Text = "";
            txtHoTen.Text = "";
            txtCMND.Text="";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtEmail.Text = "";
            txtSoDu.Text = "";
            txtSoTaiKhoan.Text = "";
        }
        #endregion

        #region Button_Click

        protected void btDongY_Click(object sender, EventArgs e)
        {
            int c = countUser();
            if (c != 1)
            {
                Ins_Info_Account();
                Ins_Info_Customer();
                Ins_Info_Payment();

                Response.Redirect("Login.aspx");
            }
            else
            {
                Response.Write("<script>alert('Tên đăng nhập đã tồn tại vui lòng chọn tên khác. Thank!!!')</script>");
                txttenDangNhap.Text = "";
            }
        }

        protected void btReset_Click(object sender, EventArgs e)
        {
            ClearData();
        }

        #endregion
    }
}